package project;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * This class will output a url to console that you can put into a browser to download a csv 
 * historical stock data from Yahoo. It will also display the resulting csv values into the console.
 * 
 * Example: new HistoricalCollector("GOOG", 12, 1, 2010, 2, 26, 2011, "d").collectHistorical();
 * 		This will get the historical data for Google (GOOG) from December 1st 2010 to February 26th, 2011.
 * 		The "d" means that it will collect the daily information (you can also use "m" for month or "y" for year).
 * @author henry
 *
 */
public class HistoricalCollector {
	private static String stock = null;
	private static int startMonth = 0;
	private static int startDay = 0;
	private static int startYear = 0;
	private static int endMonth = 0;
	private static int endDay = 0;
	private static int endYear = 0;
	private static String grain = null;
	
	HistoricalCollector(String s, int sm, int sd, int sy, int em, int ed, int ey, String g){
		stock = s;
		startMonth = sm;
		startDay = sd;
		startYear = sy;
		endMonth = em;
		endDay = ed;
		endYear = ey;
		grain = g;
	};
	
	public static void collectHistorical() throws Exception{
		String s = "http://ichart.finance.yahoo.com/table.csv?s="+stock+
			"&d="+(endMonth-1)+"&e="+endDay+"&f="+endYear+"&g="+grain+
			"&a="+(startMonth-1)+"&b="+startDay+"&c="+startYear+"&ignore=.csv";
		System.out.println(s);
		URL yahoo = new URL(s);
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
	}

}
